﻿using System;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Collections.Generic;
using System.Linq;
using System.Data.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Drawing;
using System.Text;
using System.Web.Mvc;
using System.IO;
using System.Data.Objects;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using FILSTARTUPS_CMS.Models;

namespace FILSTARTUPS_CMS.Controllers
{
    // [Authorize]
    public class CategoryController : Controller
    {
        FilStartUpsEntities cms_dbconnect = new FilStartUpsEntities();
        //
        // GET: /Category/



        public ActionResult Index()
        {

            var category = (from p in cms_dbconnect.Pages
                            join c in cms_dbconnect.Categories
                            on p.pageidPK equals c.pageidFK
                            join r in cms_dbconnect.RoutingType
                            on c.routingidFK equals r.routingidPK
                            orderby c.catidPK descending
                            select new CategoryPage
                            {
                                catidPK = c.catidPK,
                                enabled = r.routingname,
                                categorydescription = c.categorydescription,
                                categoryname = c.categoryname,
                                pagetitle = p.Pagetitle
                            }).ToList();

            ViewData["Pages"] = cms_dbconnect.Pages.OrderBy(p => p.pageidPK).ToList();

            return View(category);
        }

        [HttpPost]
        public JsonResult GetCategory(string query, int pageidFK)
        {
            string pageid = pageidFK.ToString();
            if (pageid == "" || pageid == null)
            {
                var category = (from p in cms_dbconnect.Pages
                                join c in cms_dbconnect.Categories
                                on p.pageidPK equals c.pageidFK
                                join r in cms_dbconnect.RoutingType
                                on c.routingidFK equals r.routingidPK
                                where c.categoryname.Contains(query)
                                orderby c.catidPK descending
                                select new CategoryPage
                                {
                                    catidPK = c.catidPK,
                                    enabled = r.routingname,
                                    categorydescription = c.categorydescription,
                                    categoryname = c.categoryname,
                                    pagetitle = p.Pagetitle
                                }).Distinct().ToArray();
                return Json(category, JsonRequestBehavior.AllowGet);
            }
            else
            {
                var category = (from p in cms_dbconnect.Pages
                                join c in cms_dbconnect.Categories
                                on p.pageidPK equals c.pageidFK
                                join r in cms_dbconnect.RoutingType
                                on c.routingidFK equals r.routingidPK
                                where c.pageidFK.Equals(pageidFK)
                                && c.categoryname.Contains(query)
                                orderby c.catidPK descending
                                select new CategoryPage
                                {
                                    catidPK = c.catidPK,
                                    enabled = r.routingname,
                                    categorydescription = c.categorydescription,
                                    categoryname = c.categoryname,
                                    pagetitle = p.Pagetitle
                                }).Distinct().ToArray();
                return Json(category, JsonRequestBehavior.AllowGet);
            }

        }

        // Partial View 
        public PartialViewResult SearchPage(string query, int pageidFK)
        {
            if (query == null || query == "")
            {
                var catpage = (from p in cms_dbconnect.Pages
                               join c in cms_dbconnect.Categories
                               on p.pageidPK equals c.pageidFK
                               join r in cms_dbconnect.RoutingType
                               on c.routingidFK equals r.routingidPK
                               where p.pageidPK.Equals(pageidFK)
                               select new CategoryPage
                               {
                                   catidPK = c.catidPK,
                                   enabled = r.routingname,
                                   categoryname = c.categoryname,
                                   pagetitle = p.Pagetitle
                               }).ToList();

                return PartialView("_CategoryList", catpage);
            }
            else
            {
                var catpage = (from p in cms_dbconnect.Pages
                               join c in cms_dbconnect.Categories
                               on p.pageidPK equals c.pageidFK
                               join r in cms_dbconnect.RoutingType
                               on c.routingidFK equals r.routingidPK
                               where p.pageidPK.Equals(pageidFK)
                               && c.categoryname.Equals(query)
                               select new CategoryPage
                               {
                                   catidPK = c.catidPK,
                                   enabled = r.routingname,
                                   categoryname = c.categoryname,
                                   pagetitle = p.Pagetitle
                               }).ToList();

                return PartialView("_CategoryList", catpage);
            }
        }

        [HttpGet]
        public ActionResult Create()
        {
            ViewData["pageidPK"] = cms_dbconnect.Pages.OrderBy(p => p.pageidPK).ToList();
            ViewData["routingidFK"] = cms_dbconnect.RoutingType.OrderBy(r => r.routingidPK).ToList();
            return View();
        }

        [HttpPost]
        public ActionResult Create(Category category, FormCollection form)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    category.datecreated = DateTime.Today;
                    category.pageidFK = Convert.ToInt32(form["pageidPK"].Trim());
                    category.routingidFK = Convert.ToInt32(form["routingidFK"].Trim());
                    cms_dbconnect.Categories.Add(category);
                    TempData["message"] = string.Format("{0} has been saved", category.categoryname);
                    cms_dbconnect.SaveChanges();
                    return RedirectToAction("Index");
                }
            }
            catch
            {
                //TODO ERROR
            }

            return View();
        }

        public ActionResult Edit(int id)
        {
            Category category = cms_dbconnect.Categories.Find(id);
            var page = cms_dbconnect.Pages.OrderBy(p => p.pageidPK).ToList();
            var routing = cms_dbconnect.RoutingType.OrderBy(r => r.routingidPK).ToList();

            ViewBag.pageidPK = new SelectList(page, "pageidPK", "Pagetitle", category.pageidFK);
            ViewBag.routing = new SelectList(routing, "routingidPK", "routingname", category.routingidFK);

            return View(category);
        }
        [HttpPost]
        public ActionResult Edit(Category category, FormCollection form)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    category.datecreated = DateTime.Today;
                    category.routingidFK = Convert.ToInt32(form["routing"].Trim());
                    category.pageidFK = Convert.ToInt32(form["pageidPK"].Trim());
                    cms_dbconnect.Entry(category).State = EntityState.Modified;
                    TempData["message"] = string.Format("{0} has been modified", category.categoryname);
                    cms_dbconnect.SaveChanges();
                    return RedirectToAction("Index");
                }
            }
            catch
            {
                return null;
            }
            return null;
        }

        public ActionResult Details(int id)
        {
            var category = (from c in cms_dbconnect.Categories
                            join r in cms_dbconnect.RoutingType
                            on c.routingidFK equals r.routingidPK
                            join p in cms_dbconnect.Pages
                            on c.pageidFK equals p.pageidPK
                            where c.catidPK.Equals(id)
                            select new CategoryPage
                            {
                                categorydescription = c.categorydescription,
                                categoryname = c.categoryname,
                                catidPK = c.catidPK,
                                datecreated = c.datecreated,
                                enabled = r.routingname,
                                pagetitle = p.Pagetitle

                            }).SingleOrDefault();

            return View(category);
        }
    }
}
